﻿using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Tracing;
using UnityEngine;
using UnityEngine.UI;

public class GamePanel : MonoBehaviour
{
    //拿到面板物体
    private Button btn_Pause;
    private Button btn_Play;
    private Text text_Score;
    private Text text_DiamondCount;

    private void Awake()
    {
        //监听指定的事件码    转到定义EventDefine  方法
        EventCenter.AddListener(EventDefine.ShowGamePanel,Show);
        EventCenter.AddListener<int>(EventDefine.UpdateScoreText, UpdateScore);
        EventCenter.AddListener<int>(EventDefine.updateDiamondText,UpdateDiamond);
        Init();
    }
    private void Init() 
    {
        btn_Pause = transform.Find("btn_Pause").GetComponent<Button>();
        //监听事件
        btn_Pause.onClick.AddListener(OnPauseButtonClick);
        btn_Play = transform.Find("btn_Play").GetComponent<Button>();
        btn_Play.onClick.AddListener(OnPlayButtonClick);
        text_Score = transform.Find("text_Score").GetComponent<Text>();

        text_DiamondCount = transform.Find("Diamond/text_DiamondCount").GetComponent<Text>();

        //隐藏物体
        btn_Play.gameObject.SetActive(false);
        gameObject.SetActive(false);
    }
    private void Show()
    {
        gameObject.SetActive(true);
    }
    /// <summary>
    /// 更新成绩
    /// </summary>
    private void UpdateScore(int score) 
    {
        text_Score.text = score.ToString();
    }
    /// <summary>
    /// 更新钻石的数量
    /// </summary>
    /// <param name="diamond"></param>
    private void UpdateDiamond(int diamond) 
    {
        text_DiamondCount.text = diamond.ToString();
    }
    /// <summary>
    /// 移除监听
    /// </summary>
    private void OnDestroy()
    {
        EventCenter.RemoveListener(EventDefine.ShowGamePanel,Show);
        EventCenter.RemoveListener<int>(EventDefine.UpdateScoreText,UpdateScore);
        EventCenter.RemoveListener<int>(EventDefine.updateDiamondText, UpdateDiamond);
    }
    /// <summary>
    /// 暂停按钮点击
    /// </summary>
    private void OnPauseButtonClick() 
    {
        btn_Play.gameObject.SetActive(true);
        btn_Pause.gameObject.SetActive(false);

        //TODO游戏暂停
        Time.timeScale = 0;
        GameManager.Instance.IsPaused = true;
    }
    /// <summary>
    /// 开始按钮点击
    /// </summary>
    private void OnPlayButtonClick()
    {
        btn_Pause.gameObject.SetActive(true);
        btn_Play.gameObject.SetActive(false);
        //TODO继续游戏
        Time.timeScale = 1;
        GameManager.Instance.IsPaused = false;
    }

}
